package com.kaesar.thread.ShiZhanJavaGaoBingFaSheJi2.chp3;

import com.google.common.util.concurrent.RateLimiter;

/**
 * Guava 的 RateLimiter 方法：采用 令牌桶算法 限流
 *
 * @Author: chengk
 * @Date: 2021/7/26 9:17 下午
 */
public class RateLimiterDemo {
    /**
     * 参数 permitsPerSecond 限制RateLimiter每秒只能处理两个请求
     */
    static RateLimiter limiter = RateLimiter.create(2);

    public static class Task implements Runnable {

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 50; i++) {
            // 控制流量的方法
            limiter.acquire();
            new Thread(new Task()).start();
        }
    }
}
